package io.github.biezhi.asn1;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

import org.bouncycastle.asn1.ASN1InputStream;
import org.bouncycastle.asn1.ASN1Sequence;

import java.io.IOException;
import java.util.Enumeration;

public class ASN1Parser {

    public static void main(String[] args) throws IOException {
        // 假设我们有一个DER编码的ASN.1 SEQUENCE，包含两个INTEGER字段
        byte[] derEncodedSequence = new byte[] { 0x30, 0x0A, 0x02, 0x01, 0x05, 0x02, 0x01, 0x07 };
        String fileName = "C:\\Users\\Administrator\\Desktop\\iq_3509760000_25600000.dat";

        // 将文件路径转换为Path对象
        Path fileToRead = Paths.get(fileName);

        // 使用Files.readAllBytes方法读取文件内容到字节数组
        byte[] fileContentAsBytes = Files.readAllBytes(fileToRead);

        // 解析DER编码
        ASN1InputStream asn1InputStream = new ASN1InputStream(fileContentAsBytes);
        ASN1Sequence sequence = ASN1Sequence.getInstance(asn1InputStream.readObject());

        // 遍历SEQUENCE中的元素
        Enumeration<?> enumeration = sequence.getObjects();
        while (enumeration.hasMoreElements()) {
//            DERInteger integer = DERInteger.getInstance(enumeration.nextElement());
//            System.out.println("Parsed INTEGER value: " + integer.getValue());
        }

        asn1InputStream.close();
    }
}
